#include "courier_add.h"
#include "ui_courier_add.h"

Courier_add::Courier_add(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Courier_add)
{
    socket = SocketManager::getinstance().socket();
    ui->setupUi(this);
    this->setFixedSize(440,720);
    connect(socket,&QAbstractSocket::readyRead,this,&Courier_add::recv_add);
}

Courier_add::~Courier_add()
{
    delete ui;
}

void Courier_add::on_pushButton_clicked()
{
    sendRig ddata;
    ddata.type=3;
    const char* acc = ui->lineEdit->text().toUtf8().constData();
    const char* id = ui->lineEdit_2->text().toUtf8().constData();
    const char* ctele = ui->lineEdit_3->text().toUtf8().constData();
    const char* rtele = ui->lineEdit_4->text().toUtf8().constData();
    strcpy(ddata.Account,acc);
    strcpy(ddata.ID,id);
    strcpy(ddata.tele,ctele);
    strcpy(ddata.teletwo,rtele);
    QByteArray message;
    message.append((char*)&ddata,sizeof (sendRig));
    socket->write(message);
}

void Courier_add::recv_add()
{
    if(!this->isActiveWindow()){
        return;
    }
    sendRig  rd;
    while (socket->bytesAvailable() >= sizeof(sendRig)) {
            socket->read(reinterpret_cast<char*>(&rd), sizeof(sendRig));
    }
//    rd = (sendRig *)socket->readAll().data();
    if(rd.type == 3){
        if(rd.IC == 1)
        {
            emit(addtomain());
        }else
        {
            //录入失败
            QMessageBox::warning(this,"录入失败","订单信息录入失败","返回 ");
        }
    }
}

void Courier_add::on_pushButton_2_clicked()
{
    emit(addtomain());
}
